package org.csu.ssm_backend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.csu.ssm_backend.domain.Account;
import org.csu.ssm_backend.domain.Product;
import org.csu.ssm_backend.persistence.AccountMapper;
import org.csu.ssm_backend.persistence.ProductMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MapperScan("org.csu.ssm_backend.persistence")
public class AccountService {

    @Resource
    private AccountMapper accountMapper;

    AccountService(){
    }

    public Account getAccount(String userid,String password){
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userid).eq("password",password);
        return accountMapper.selectOne(queryWrapper);
    }

    public Account getAccountByPhone(String phone){
        QueryWrapper<Account> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return accountMapper.selectOne(queryWrapper);
    }

    public void insertAccount(String userid,String password,String email){
        Account account=new Account();
        account.setEmail(email);
        account.setUserid(userid);
        account.setPassword(password);
        accountMapper.insert(account);
    }

    public void insertAccount(Account account){
        accountMapper.insert(account);
    }

    public void updateAccount(Account account){
        UpdateWrapper<Account> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("userid",account.getUserid());
        accountMapper.update(account,updateWrapper);
    }
}
